package com.my.springbootvalidation.bean;

import com.my.springbootvalidation.group.IdCheck;
import com.my.springbootvalidation.group.InsertCheck;
import jakarta.persistence.*;
import jakarta.validation.constraints.Email;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Pattern;
import jakarta.validation.constraints.Size;
import lombok.Data;


/**
 * @Author marshal
 * @Date 2021-08-03 9:27 PM
 */
@Entity(name = "validation")
@Data
public class ValidationBean {

    /**
     * ID 只有在更新和删除的时候才校验
     */
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY) // 指定ID自增
    @NotNull(message = "ID不能为空", groups = IdCheck.class)
    private Long id;

    /**
     * 不为空校验，长度校验
     */
    @Column(nullable = false)
    @NotNull(message = "姓名不能为空", groups = InsertCheck.class)
    @Size(max = 40, min = 6, message = "姓名不合法【姓名字段长度必须在6-40之间】", groups =InsertCheck.class)
    private String name;

    /**
     * 通过正则表达式来校验
     */
    @Column
    // @Pattern注解不支持Integer类型
//    @Pattern(regexp = "^^((1[0-5])|[1-9])?\\d$", message = "年龄不合法【年龄必须是1~159之间的数字", groups = InsertCheck.class)
    private Integer age;

    /**
     * 邮箱校验
     */
    @Column
    @Email(message = "邮箱不合法", groups = InsertCheck.class)
    private String email;

    @Column
    @Pattern(regexp = "^([1-9]|[1-9]\\\\d|1\\\\d{2}|2[0-4]\\\\d|25[0-5])(\\\\.(\\\\d|[1-9]\\\\d|1\\\\d{2}|2[0-4]\\\\d|25[0-5])){3}$", message = "IP不合法", groups = InsertCheck.class)
    private String ip;
}
